/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 11000;
struct node
{
    ll y, cal, flow, next_s;
} e[maxn];
ll n, m, s, t;
ll head[maxn], cnt;
bool vis[maxn];
ll d[maxn];
void init()
{
    memset(head, 0, sizeof(head));
    cnt = 2;
}

void add(ll x, ll y, ll cal, ll flow)
{
    e[cnt].y = y;
    e[cnt].cal = cal;
    e[cnt].flow = flow;
    e[cnt].next_s = head[x];
    head[x] = cnt++;
    return;
}

bool bfs()
{
    // vis[s]=true;
    // memset(d, 0, sizeof(d));
    for (ll i = 1; i <= n; i++)
        d[i] = 0;
    queue<ll> p;
    d[s] = 1;
    p.push(s);
    while (!p.empty())
    {
        ll u = p.front();
        p.pop();
        for (ll i = head[u]; i; i = e[i].next_s)
        {
            ll v = e[i].y;
            if (d[v] || e[i].cal <= e[i].flow)
                continue;
            d[v] = d[u] + 1;
            p.push(v);
        }
    }
    return d[t];
}

ll dinic(ll x, ll cur_flow)
{
    if (x == t || cur_flow == 0)
    {
        return cur_flow;
    }
    ll flow = 0, f;
    for (ll i = head[x]; i; i = e[i].next_s)
    {
        ll v = e[i].y;
        if (d[v] == d[x] + 1 && e[i].cal - e[i].flow > 0)
        {
            f = dinic(v, min(cur_flow, e[i].cal - e[i].flow));
            if (f == 0)
                d[v] = 0; //重要剪枝，去掉增广完毕的点
            e[i].flow += f;
            e[i ^ 1].flow -= f;
            flow += f;
            cur_flow -= f;
            if (cur_flow == 0)
            {
                return flow;
            }
        }
    }
    return flow;
}

int main()
{
    IOS;
    init();
    cin >> n >> m >> s >> t;
    ll x, y, cal;
    for (ll i = 0; i < m; i++)
    {
        cin >> x >> y >> cal;
        add(x, y, cal, 0);
        add(y, x, 0, 0);
    }
    ll max_flow = 0;
    while (bfs())
    {
        max_flow += dinic(s, INF);
    }
    cout << max_flow << endl;
    getchar();
    getchar();
    return 0;
}